﻿//-----------------------------------------------------------------------
// <copyright file="ScheduleHistory.Data.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings
using System;
using System.Data;
using FastNET.CleanAct.Common.Repository;
using FastNET.CleanAct.Common.Repository.Interfaces.DataInterfaces.ManagementEngine;
#endregion

namespace FastNET.CleanAct.Client.ManagerEngineData.Entities
{
    /// <summary>
    /// Schedule History Entity Data Class
    /// </summary>
    internal partial class ScheduleHistory : IScheduleHistoryData
    {
        private bool _isDirty;

        /// <summary>
        /// Initializes a new instance of the DynamicSetting class
        /// </summary>
        public ScheduleHistory()
        {
            ScheduleHistoryID = Guid.NewGuid();
            _isDirty = false;
            PropertyChanged += EventHandler_PropertyChanged;
        }

        #region State Management

        private void EventHandler_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            _isDirty = true;
        }

        /// <summary>
        /// Gets a value indicating whether or not this instance has been modified
        /// </summary>
        public bool IsDirty
        {
            get
            {
                if (IsNew)
                {
                    return true;
                }

                return _isDirty;
            }

            internal set
            {
                _isDirty = false;
            }
        }

        /// <summary>
        /// Gets a value indicating whether or not this instance is new
        /// </summary>
        public bool IsNew
        {
            get
            {
                // Before it is attached if the key is null then we are new
                if (EntityState == EntityState.Detached)
                {
                    if (EntityKey == null)
                    {
                        return true;
                    }
                }

                return EntityState == EntityState.Added;
            }
        }

        /// <summary>
        /// Gets a value indicating whether or not this instance has been deleted
        /// </summary>
        public bool IsDeleted
        {
            get
            {
                return EntityState == EntityState.Deleted;
            }
        }

        #endregion

        #region Identity Management

        /// <summary>
        /// Gets an Identity based reference for this item
        /// </summary>
        public Identity ID
        {
            get { return ScheduleHistoryID; }
        }

        #endregion

        #region ID Reference Properties

        /// <summary>
        /// Gets or sets the associated Schedule ID for this setting
        /// </summary>
        public Identity ScheduleID
        {
            get
            {
                return (Guid)ScheduleReference.EntityKey.EntityKeyValues[0].Value;
            }

            set
            {
                if (!value.CanFitInGuid())
                {
                    throw new InvalidCastException("Identity is not of type Guid");
                }

                ScheduleReference.EntityKey = new EntityKey("ManagerDataEntities.Schedules", "ScheduleID", (Guid)value);
            }
        }

        #endregion
    }
}
